package com.sservice.service.service.impl;

import com.sservice.model.dao.IAddressDao;
import com.sservice.model.entity.Address;
import com.sservice.service.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by suweiming on 2017/12/20.
 */


@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    IAddressDao dao;

    /**
     * @param address 地址ID
     * @return
     */
    @Override
    public int setDefault(Address address) {
        Address address1 = dao.selectByPrimaryKey(address.getId());
        if (address.getDefault_().equals("1")) {
            Address address2 = new Address();
            address2.setOwnerid(address1.getOwnerid());
            address2.setDefault_("0");
            dao.updateByOwnIdSelective(address2);
        }
        address1.setDefault_(address.getDefault_());
        return dao.updateByPrimaryKeySelective(address1);
    }

    @Override
    public int setPaper(Address address) {
        Address address1 = dao.selectByPrimaryKey(address.getId());
        if (address.getDefault_().equals("1")) {
            Address address2 = new Address();
            address2.setOwnerid(address1.getOwnerid());
            address2.setPaper("0");
            dao.updateByOwnIdSelective(address2);
        }
        address1.setPaper(address.getDefault_());
        return dao.updateByPrimaryKeySelective(address1);
    }

    @Override
    public int setReceive(Address address) {
        Address address1 = dao.selectByPrimaryKey(address.getId());
        Address address2 = new Address();
        address2.setOwnerid(address1.getOwnerid());
        address2.setReceive(address.getDefault_());
        address2.setId(address.getId());
        return dao.updateByPrimaryKeySelective(address2);
    }
}
